package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.entity.MjStatistics;
import com.moji.entity.MjVisitRecord;
import com.moji.entity.dto.QueryStatisticsDto;
import com.moji.entity.vo.ResultVo;
import com.moji.entity.vo.ResultVo2;
import com.moji.entity.vo.StatisticsVo;
import com.moji.mapper.MjStatisticsMapper;
import com.moji.service.MjStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.service.MjVisitRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据统计表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-11-06
 */
@Service
public class MjStatisticsServiceImpl extends ServiceImpl<MjStatisticsMapper, MjStatistics> implements MjStatisticsService {

    @Resource
    private MjVisitRecordService mjVisitRecordService;

    @Override
    public StatisticsVo hits(QueryStatisticsDto dto) {
        StatisticsVo statisticsVo=new StatisticsVo();
        LambdaQueryWrapper<MjStatistics> wrapper=new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getMenuType())){
            wrapper.eq(MjStatistics::getMenuType,dto.getMenuType());
        }
        if(ObjectUtil.isNotEmpty(dto.getLoginWay())){
            wrapper.eq(MjStatistics::getLoginWay,dto.getLoginWay());
        }
        if(ObjectUtil.isNotEmpty(dto.getTitleId())){
            wrapper.eq(MjStatistics::getTitleId,dto.getTitleId());
        }
        if(ObjectUtil.isNotEmpty(dto.getStartDate())){
            wrapper.ge(MjStatistics::getCreateTime,dto.getStartDate());
        }
        if(ObjectUtil.isNotEmpty(dto.getEndDate())){
            wrapper.le(MjStatistics::getCreateTime,dto.getEndDate());
        }
        wrapper.eq(MjStatistics::getType,1);
        List<MjStatistics> statisticsList=this.list(wrapper);
        if(!statisticsList.isEmpty()){
            if(("login").equals(dto.getMenuType())){//登录
                int codeHits=0;//验证码
                int accountHits=0;//密码
                for(MjStatistics one:statisticsList){
                    if(one.getLoginWay() !=null && one.getLoginWay()==1 && one.getType()==1){
                        codeHits=codeHits+one.getHits();
                    }
                    if(one.getLoginWay() !=null && one.getLoginWay()==2 && one.getType()==2){
                        accountHits=accountHits+one.getHits();
                    }
                }
                ResultVo vo=new ResultVo();
                List<ResultVo2> resultVo2s=new ArrayList<>();
                vo.setTitleId(statisticsList.get(0).getTitleId()+"");
                vo.setTitleName(statisticsList.get(0).getTitleName()+"");
                Map<String,Integer> map=new HashMap<>();
                map.put("codeHits",codeHits);
                map.put("accountHits",accountHits);
                ResultVo2 resultVo2=new ResultVo2();
                resultVo2.setButtonCode("codeHits");
                resultVo2.setButtonName("验证码点击量");
                resultVo2.setHitCount(codeHits);
                resultVo2s.add(resultVo2);
                ResultVo2 resultVo3=new ResultVo2();
                resultVo3.setButtonCode("accountHits");
                resultVo3.setButtonName("账号点击量");
                resultVo3.setHitCount(accountHits);
                resultVo2s.add(resultVo3);
                vo.setResultVo2List(resultVo2s);

                //ResultVo vo1=new ResultVo();
                //vo1.setResultVo2List(resultVo3);
                //vo.setHitsMap(map);
                List<ResultVo> resultVos=new ArrayList<>();
                resultVos.add(vo);
                statisticsVo.setResultVoList(resultVos);
                return statisticsVo;
            }
            if(("register").equals(dto.getMenuType())){//注册
                int hitsCount=statisticsList.stream().mapToInt(MjStatistics::getHits).sum();
                ResultVo vo=new ResultVo();
                vo.setTitleId(statisticsList.get(0).getTitleId()+"");
                vo.setTitleName(statisticsList.get(0).getTitleName()+"");
                //Map<String,Integer> map=new HashMap<>();
                //map.put(statisticsList.get(0).getButtonCode(),hitsCount);
                ResultVo2 resultVo2=new ResultVo2();
                resultVo2.setButtonCode(statisticsList.get(0).getButtonCode());
                resultVo2.setButtonName(statisticsList.get(0).getButtonName());
                resultVo2.setHitCount(hitsCount);
                List<ResultVo2> resultVo2s=new ArrayList<>();
                resultVo2s.add(resultVo2);
                vo.setResultVo2List(resultVo2s);
                List<ResultVo> resultVos=new ArrayList<>();
                resultVos.add(vo);
                statisticsVo.setResultVoList(resultVos);
                return statisticsVo;
            }
//                Map<String, Integer> map =statisticsList.stream()
//                .collect(Collectors.groupingBy(MjStatistics::getButtonCode,
//                        Collectors.summingInt(MjStatistics::getHits)));
                Map<String, List<MjStatistics>> groupedProducts =
                        statisticsList.stream()
                        .collect(Collectors.groupingBy(MjStatistics::getButtonCode));
            List<ResultVo> resultVos=new ArrayList<>();
            ResultVo vo=new ResultVo();
            vo.setTitleId(statisticsList.get(0).getTitleId()+"");
            vo.setTitleName(statisticsList.get(0).getTitleName()+"");
            List<ResultVo2> resultVo2s=new ArrayList<>();
            groupedProducts.forEach((button, list) -> {

                //System.out.println("Total price for " + fruit + " is: " + list);
                int count=0;
                for(MjStatistics mjStatistics:list){
                    count=count+mjStatistics.getHits();
                }
                ResultVo2 resultVo2=new ResultVo2();
                resultVo2.setButtonCode(list.get(0).getButtonCode());
                resultVo2.setButtonName(list.get(0).getButtonName());
                resultVo2.setHitCount(count);
                resultVo2s.add(resultVo2);

            });
            vo.setResultVo2List(resultVo2s);
            resultVos.add(vo);
            statisticsVo.setResultVoList(resultVos);

        }
        return statisticsVo;
    }

    @Override
    public StatisticsVo visitPerson(QueryStatisticsDto dto) {
        StatisticsVo statisticsVo=new StatisticsVo();
        LambdaQueryWrapper<MjStatistics> wrapper=setWrapper0(dto);
        wrapper.eq(MjStatistics::getType,3);
        List<MjStatistics> statisticsList=this.list(wrapper);
        if(statisticsList.isEmpty()){
            return statisticsVo;
        }
            statisticsList.forEach(mjStatistics -> {
                int count=mjVisitRecordService.count(setWrapper(mjStatistics.getVisitPerson(),dto));
                String avgTime=getAvgTime(mjStatistics.getStopTime(),count);
                mjStatistics.setAvgStopTime(avgTime);
            });
        if("login".equals(dto.getMenuType()) || "register".equals(dto.getMenuType())
                || "connect".equals(dto.getMenuType()) || "footer".equals(dto.getMenuType())){
            return statisticsVo;
        }
        statisticsVo.setVisitRecordList(statisticsList);
        return statisticsVo;
    }

    private String getAvgTime(Integer stopTime, int count) {
        double result = (double) stopTime / count;
        String formattedResult = String.format("%.2f", result);
        return formattedResult;
    }

    @Override
    public StatisticsVo pvUv(QueryStatisticsDto dto) {
        StatisticsVo statisticsVo=new StatisticsVo();
        if("login".equals(dto.getMenuType()) || "register".equals(dto.getMenuType())
                || "menu".equals(dto.getMenuType()) || "connect".equals(dto.getMenuType())
        || "footer".equals(dto.getMenuType())){
            return statisticsVo;
        }
        LambdaQueryWrapper<MjStatistics> wrapper=setWrapper0(dto);
        wrapper.eq(MjStatistics::getType,2);
        List<MjStatistics> statisticsList=this.list(wrapper);
        if(ObjectUtil.isNotEmpty(statisticsList)){
            Map<String, Integer> map =statisticsList.stream()
                    .collect(Collectors.groupingBy(MjStatistics::getNianMonthDay,
                            Collectors.summingInt(MjStatistics::getVisits)));
            List<ResultVo> resultVos=new ArrayList<>();
            ResultVo vo=new ResultVo();
            vo.setTitleId(statisticsList.get(0).getTitleId()+"");
            vo.setTitleName(statisticsList.get(0).getTitleName()+"");
            vo.setPvMap(map);
            resultVos.add(vo);
            //statisticsVo.setResultVoList(resultVos);

            Map<String, Integer> uvMap1=new HashMap<>();
            Map<String, Long> uvMap =statisticsList.stream()
                    .collect(Collectors.groupingBy(MjStatistics::getNianMonthDay,
                            Collectors.counting()));
            uvMap.forEach((str, value) -> {
                uvMap1.put(str,Math.toIntExact(value));
                //System.out.println("Total price for " + fruit + " is: " + list);
            });
            //ResultVo vo1=new ResultVo();
            //vo1.setTitleId(statisticsList.get(0).getTitleId()+"");
            //vo1.setTitleName(statisticsList.get(0).getTitleName()+"");
            vo.setUvMap(uvMap1);
            //resultVos.add(vo1);
            statisticsVo.setResultVoList(resultVos);
            statisticsVo.setResultVoList(resultVos);
        }
        return statisticsVo;
    }

    @Override
    public List<Map<String,String>> selectTitle(String menuType) {
        List<Map<String,String>> resList=new ArrayList<>();
        if("menu".equals(menuType)){
            Map<String, String> resMap1=new HashMap<>();
            resMap1.put("key","solution");
            resMap1.put("label","解决方案");
            Map<String, String> resMap2=new HashMap<>();
            resMap2.put("key","product");
            resMap2.put("label","产品");
            Map<String, String> resMap3=new HashMap<>();
            resMap3.put("key","apis");
            resMap3.put("label","API");
            Map<String, String> resMap4=new HashMap<>();
            resMap4.put("key","about");
            resMap4.put("label","关于");
            resList.add(resMap1);
            resList.add(resMap2);
            resList.add(resMap3);
            resList.add(resMap4);
        }
        if("solution".equals(menuType)){
            Map<String, String> resMap1=new HashMap<>();
            resMap1.put("key","guidao");
            resMap1.put("label","轨道交通气象服务解决方案");
            Map<String, String> resMap2=new HashMap<>();
            resMap2.put("key","qixiang");
            resMap2.put("label","航空气象服务解决方案");
            Map<String, String> resMap3=new HashMap<>();
            resMap3.put("key","gaosu");
            resMap3.put("label","高速公路气象服务解决方案");
            resList.add(resMap1);
            resList.add(resMap2);
            resList.add(resMap3);
        }

        if("product".equals(menuType)){
            Map<String, String> resMap1=new HashMap<>();
            resMap1.put("key","api");
            resMap1.put("label","数据API产品");
            Map<String, String> resMap2=new HashMap<>();
            resMap2.put("key","guidao");
            resMap2.put("label","轨道交通气象服务产品");
            Map<String, String> resMap3=new HashMap<>();
            resMap3.put("key","hangkong");
            resMap3.put("label","航空气象服务产品");
            Map<String, String> resMap4=new HashMap<>();
            resMap4.put("key","saishi");
            resMap4.put("label","赛事气象服务产品");
            resList.add(resMap1);
            resList.add(resMap2);
            resList.add(resMap3);
            resList.add(resMap4);

        }
        if("about".equals(menuType)){
            Map<String, String> resMap1=new HashMap<>();
            resMap1.put("key","about");
            resMap1.put("about","关于我们");
            Map<String, String> resMap2=new HashMap<>();
            resMap2.put("key","news");
            resMap2.put("news","新闻中心");
            resList.add(resMap1);
            resList.add(resMap2);
        }
        return resList;
    }

    @Override
    public Map<String,Long> countOver3s(QueryStatisticsDto dto) {
        LambdaQueryWrapper<MjVisitRecord> wrapper=new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getMenuType())) {
            wrapper.eq(MjVisitRecord::getMenuType, dto.getMenuType());
        }
        if(ObjectUtil.isNotEmpty(dto.getStartDate())){
            wrapper.ge(MjVisitRecord::getCreateTime,dto.getStartDate());
        }
        if(ObjectUtil.isNotEmpty(dto.getEndDate())){
            wrapper.le(MjVisitRecord::getCreateTime,dto.getEndDate());
        }
        wrapper.ge(MjVisitRecord::getStopTime,"3");
        List<MjVisitRecord> list=mjVisitRecordService.list(wrapper);
        if(list.isEmpty()){
            return null;
        }
        Map<String, Long> map =list.stream()
                .collect(Collectors.groupingBy(MjVisitRecord::getNianMonthDay,
                        Collectors.counting()));
        return map;
    }

    private LambdaQueryWrapper<MjVisitRecord> setWrapper(String visitPerson,QueryStatisticsDto dto){
        LambdaQueryWrapper<MjVisitRecord> wrapper=new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getMenuType())){
            wrapper.eq(MjVisitRecord::getMenuType,dto.getMenuType());
        }
        if(ObjectUtil.isNotEmpty(visitPerson)){
            wrapper.eq(MjVisitRecord::getVisitPerson,visitPerson);
        }
        if(ObjectUtil.isNotEmpty(dto.getTitleId())){
            wrapper.eq(MjVisitRecord::getTitleId,dto.getTitleId());
        }
        if(ObjectUtil.isNotEmpty(dto.getStartDate())){
            wrapper.ge(MjVisitRecord::getCreateTime,dto.getStartDate());
        }
        if(ObjectUtil.isNotEmpty(dto.getEndDate())){
            wrapper.le(MjVisitRecord::getCreateTime,dto.getEndDate());
        }
        return wrapper;
    }
    private LambdaQueryWrapper<MjStatistics> setWrapper0(QueryStatisticsDto dto){
        LambdaQueryWrapper<MjStatistics> wrapper=new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getMenuType())){
            wrapper.eq(MjStatistics::getMenuType,dto.getMenuType());
        }
        if(ObjectUtil.isNotEmpty(dto.getTitleId())){
            wrapper.eq(MjStatistics::getTitleId,dto.getTitleId());
        }
        if(ObjectUtil.isNotEmpty(dto.getLoginWay())){
            wrapper.eq(MjStatistics::getLoginWay,dto.getLoginWay());
        }
        if(ObjectUtil.isNotEmpty(dto.getStartDate())){
            wrapper.ge(MjStatistics::getCreateTime,dto.getStartDate());
        }
        if(ObjectUtil.isNotEmpty(dto.getEndDate())){
            wrapper.le(MjStatistics::getCreateTime,dto.getEndDate());
        }
        return wrapper;
    }

    public static void main(String[] args){
        List<ResultVo> resultVos=new ArrayList<>();
        ResultVo vo4=new ResultVo();
        vo4.setTitleId("3");
        vo4.setHits(12);
        vo4.setDate("20240102");
        resultVos.add(vo4);
        ResultVo vo1=new ResultVo();
        vo1.setTitleId("1");
        vo1.setHits(5);
        vo1.setDate("20240102");
        resultVos.add(vo1);
        ResultVo vo2=new ResultVo();
        vo2.setTitleId("4");
        //vo2.setHits(null);
        vo2.setDate("20240103");
        resultVos.add(vo2);

        ResultVo vo3=new ResultVo();
        vo3.setDate("20240104");
        vo3.setTitleId("2");
        vo3.setHits(12);
        resultVos.add(vo3);

//        Map<String, Integer> groupedProducts =resultVos.stream()
//                .collect(Collectors.groupingBy(ResultVo::getTitleId,
//                        Collectors.summingInt(ResultVo::getHits)));

//        Map<String, List<ResultVo>> groupedProducts =
//                resultVos.stream()
//                        .collect(Collectors.groupingBy(ResultVo::getTitleId));
//        List<ResultVo> resultVos1=resultVos.stream().filter(s->ObjectUtil.isNotEmpty(s.getHits()))
//                .sorted(Comparator.comparing(ResultVo::getTitleId)).collect(Collectors.toList());
                Map<String, Long> groupedProducts =resultVos.stream()
                .collect(Collectors.groupingBy(ResultVo::getDate,
                        Collectors.counting()));
        groupedProducts.forEach((fruit, list) -> {
            System.out.println("Total price for " + fruit + " is: " + list);
        });

    }
}
